define(function (require) {


    var JuicerCommand = {
        /**
         * init
         */
        init: function () {
            this.registerJuicerFunc();
        },
        /**
         * registerJuicerFunc
         */
        registerJuicerFunc: function () {
            var self = this;
            juicer.register('checkOrderPayType', self.checkOrderPayType);
            juicer.register('checkOrderType', self.checkOrderType);
            juicer.register('formatDate', self.formatDate);
            juicer.register('checkFundType', self.checkFundType);
            juicer.register('sumSalePrice', self.sumSalePrice);
            juicer.register('empty', self.empty);
            juicer.register('getLogisticsStatus', self.getLogisticsStatus);
            juicer.register('cancelledBy', self.cancelledBy);
            juicer.register('checkDeliveryType', self.checkDeliveryType);
            juicer.register('renderSendLogisticsnos', self.renderSendLogisticsnos);
            juicer.register('showOrderType', self.showOrderType);
            juicer.register('showChannel', self.showChannel);
        },
        getLogisticsStatus: function (logisticsStatus) {
            var logisticsStatusStr = "未知";
            if (logisticsStatus == 1) { //订单物流状态 1 待配送 2 在途中 3 已确认 5 待抢单
                logisticsStatusStr = "待配送";
            } else if (logisticsStatus == 2) {
                logisticsStatusStr = "在途中";
            } else if (logisticsStatus == 3) {
                logisticsStatusStr = "确认送达";
            } else if (logisticsStatus == 4) {
                logisticsStatusStr = "退单";
            } else if (logisticsStatus == 5) {
                logisticsStatusStr = "待抢单";
            } else if (logisticsStatus == 6) {
                logisticsStatusStr = "拒绝签收";
            } else if (logisticsStatus == 7) {
                logisticsStatusStr = "取消订单";
            } else if (logisticsStatus == 8) {
                logisticsStatusStr = "确认退货";
            } else if (logisticsStatus == 9) {
                logisticsStatusStr = "退货中";
            } else if (logisticsStatus == 10) {
                logisticsStatusStr = "问题件";
            }
            return logisticsStatusStr;
        },
        /**
         *
         * @param data
         * @returns {number}
         */
        sumSalePrice: function (data) {
            var sum = 0;
            $.each(data, function (index, item) {
                if (item.fundType != 1 && item.fundType != 7) {
                    sum += item.sellerSubsidy;
                    sum += item.platformSubsidy
                }
            });
            sum = (sum / 100).toFixed(2);
            return sum;
        },
        /**
         *
         * @param type  1 满减:2, 特价:3, 红包:4, 套餐:5, 运费:6,
         * @returns {string}
         */
        checkFundType: function (type) {
            var typeName = "补贴";
            if (type == 2) {
                typeName = '满减优惠'
            } else if (type == 3) {
                typeName = '特价优惠'
            } else if (type == 4) {
                typeName = '红包优惠'
            } else if (type == 5) {
                typeName = '套餐优惠'
            } else if (type == 6) {
                typeName = '运费优惠'
            } else if (type == 7) {
                typeName = '自营商品货款'
            } else if (type == 8) {
                typeName = '闪电侠服务费'
            } else if (type == 9) {
                typeName = '配送费'
            } else if (type == 10) {
                typeName = '售中退款'
            } else if (type == 11) {
                typeName = '售后退款'
            }
            return typeName;
        },
        /**
         *
         * @param type 1 待支付 2 待发货 3 待确认 4 成功 5 失败 6 卖家拒单 7 待提货
         * @returns {string} isAccept: 0—待接单 1—已接单
         */
        checkOrderType: function (param) {
            var self = this;
            var type = param.status;
            var cancelledBy = param.cancelledBy;
            var isAccept = param.isAccept;
            var deliveryType = param.deliveryType;
            if (deliveryType == 0) {
                deliveryType = "(商家自送)";
            } else if (deliveryType == 1) {
                deliveryType = "(闪电侠送)";
            } else if (deliveryType == 2) {
                deliveryType = "(物流配送)";
            } else if (deliveryType == 3) {
                deliveryType = "(第三方配送)" + param.platformName;
            }
            function _cancelledBy(type) {
                var typeName = "";
                if (type == 0) {
                    typeName = '买家取消';
                } else if (type == 1) {
                    typeName = '系统取消';
                } else if (type == 2) {
                    typeName = '售后取消';
                } else if (type == 10) {
                    typeName = '卖家接单超时取消';
                } else if (type == 11) {
                    typeName = '卖家发货超时取消'
                } else if (type == 12) {
                    typeName = '卖家拒绝导致取消订单'
                } else if (type == 13) {
                    typeName = '拼团失败'
                } else if (type == 14) {
                    typeName = '用户拒绝签收'
                } else if (type == 102) {
                    typeName = '拼团中只剩一人取消'
                } else if (type == 104) {
                    typeName = '商家同意退款'
                } else if (type == 105) {
                    typeName = '系统超时退款'
                } else {
                    typeName = '失败';
                }
                return typeName;
            }

            var typeName = "";
            if (type == 1) {
                typeName = '待支付'
            } else if (type == 2) {
                if (isAccept == 0) {
                    typeName = '待接单'
                } else {
                    if (param.isDownloadOrderForLogistics == true) {
                        typeName = "仓库已导单，打包中";
                    } else {
                        var _typeName = JuicerCommand.getLogisticsStatus(param.logisticsStatus);
                        if (_typeName != "未知") {
                            typeName = _typeName + deliveryType;
                        } else {
                            typeName = '待发货' + deliveryType;
                        }
                    }
                }
            } else if (type == 3) {
                var _typeName = JuicerCommand.getLogisticsStatus(param.logisticsStatus);
                if (_typeName != "未知") {
                    typeName = _typeName + deliveryType;
                } else {
                    typeName = '待买家确认' + deliveryType;
                }
            } else if (type == 4) {
                typeName = '交易成功'
            } else if (type == 5) {
                typeName = _cancelledBy(cancelledBy);
            } else if (type == 6) {
                typeName = '卖家拒单' + '(' + param.rejectReason + ')';
            } else if (type == 7) {
                typeName = '待提货'
            }
            return typeName;
        },
        cancelledBy: function (type) {
            var typeName = "";
            if (type == 0) {
                typeName = '买家取消';
            } else if (type == 1) {
                typeName = '系统取消';
            } else if (type == 2) {
                typeName = '小二取消';
            } else if (type == 10) {
                typeName = '卖家接单超时取消';
            } else if (type == 11) {
                typeName = '卖家发货超时取消'
            } else if (type == 12) {
                typeName = '卖家拒绝导致取消订单'
            } else if (type == 102) {
                typeName = '拼团中只剩一人取消'
            } else {
                typeName = '失败';
            }
            return typeName;
        },
        empty: function (str) {
            if (!str) {
                return "";
            } else {
                return str;
            }
        },
        /**
         *
         * @param str
         * @returns {*}
         */
        formatDate: function (str) {
            //return moment(new Date(str)).format('YYYY-MM-DD HH:mm:ss');
            return CRM.formatDate(new Date(str));
        },
        checkOrderPayType: function (item) {
            if (item.payType == 2) {
                if (item.onlinePayType == 2) {
                    return '支付宝支付';
                } else if (item.onlinePayType == 3) {
                    return '微信支付';
                } else if (item.onlinePayType == 10) {
                    return '饿了么支付';
                }else if (item.onlinePayType == 11) {
                    return '美团支付';
                }else if (item.onlinePayType == 12) {
                    return '手淘支付';
                }else{
                    return '在线支付';
                }
            } else if (item.payType == 3) {
                return '货到付款';
            }
        },
        checkDeliveryType: function (deliveryType) {
            if (deliveryType == 0) {
                return "商家自送";
            } else if (deliveryType == 1) {
                return "闪电侠送";
            } else if (deliveryType == 2) {
                return "物流配送";
            } else if (deliveryType == 3) {
                return "第三方配送";
            }
        },
        /**
         * 订单类型
         * @param orderType
         * @param outWay
         * @returns {string}
         */
        showOrderType: function (orderType, onlinePayType) {
            var returnStr = "普通订单";
            onlinePayType = onlinePayType || 3;
            if (onlinePayType == 10) {
                returnStr = "饿了么订单";
            }else if(onlinePayType == 11) {
                returnStr = "美团订单";
            }else if(onlinePayType == 12) {
                returnStr = "手淘订单";
            }else {
                if (orderType == 2) {
                    returnStr = "拼团订单";
                } else if (orderType == 3) {
                    returnStr = "分销订单";
                } else if (orderType == 4) {
                    returnStr = "预售订单";
                } else if (orderType == 5) {
                    returnStr = "次日达订单";
                }else if (orderType == 6) {
                    returnStr = "pos订单";
                }

            }
            return returnStr;
        },
        /**
         * 預售单物流信息处理
         * @param item
         */
        renderSendLogisticsnos: function (item) {
            var html = "";
            CRM.request({
                "type": 'POST',
                "url": CRM.getOuterUrl("/logisticscentral/crm/stockout/queryByBizOrderId"),
                "dataType": "json",
                "async": false,
                contentType: "application/json",
                "data": JSON.stringify({
                    "bizOrderId": item.bizOrderId
                }),
                "success": function (req) {
                    if (req.status) {
                        if (req.entry && req.entry.length > 0) {
                            var _canModify = false;
                            $.each(req.entry, function (index, entry) {
                                var logisticsStatus = "";
                                if (item.logisticsDOList) {
                                    $.each(item.logisticsDOList, function (_index, logistics) {
                                        if (logistics.logisticsNo == entry.logisticsNo) {
                                            logisticsStatus = JuicerCommand.checkSendLogisticsnos(logistics.status);
                                        }
                                    });
                                }
                                var checkboxHtml = "<br/>";
                                if (!entry.printStatus && !entry.isCancle) {//未打印状态
                                    _canModify = true;
                                    var dataStr = "";
                                    dataStr += " data-buyernick='" + entry.buyerNick + "' ";
                                    dataStr += " data-buyermobile='" + entry.buyerMobile + "' ";
                                    dataStr += " data-districtname='" + entry.districtName + "' ";
                                    dataStr += " data-buyeraddress='" + entry.buyerAddress + "' ";
                                    dataStr += " data-bizorderid='" + entry.bizOrderId + "' ";
                                    dataStr += " data-buyercitycode='" + entry.buyerCityCode + "' ";
                                    checkboxHtml += "<input type='checkbox' value='" + entry.stockOutId + "' id='checkbox_" + entry.stockOutId + "'" + dataStr + "/> ";
                                }
                                if (logisticsStatus && "待发货" != logisticsStatus) {
                                    html += "<a id='pre_a_" + entry.logisticsNo + "' href=javascript:queryLogistics('" + entry.logisticsNo + "','" + entry.logisticsCompanyCode + "')>";
                                }
                                html += checkboxHtml + "<label for='checkbox_" + entry.stockOutId + "'>";
                                if (entry.logisticsCompanyName) {
                                    html += entry.logisticsCompanyName
                                }
                                if (entry.logisticsNo) {//物流单号
                                    html += "[" + entry.logisticsNo + "]";
                                } else {
                                    html += "[还未生成物流单号]"
                                }

                                if (entry.isCancle) {
                                    html += "(已取消)";
                                } else {
                                    var reSendHTML = "";
                                    if (entry.isResend) {
                                        reSendHTML += "--补发单";
                                    }
                                    if (!entry.printStatus) {
                                        html += "(未打印" + reSendHTML + ")";
                                    } else if (entry.printStatus && !entry.submitStatus) {
                                        html += "(已打印，物流未下单" + reSendHTML + ")";
                                    } else if (entry.printStatus && entry.submitStatus) {
                                        html += "(已下单," + logisticsStatus + "" + reSendHTML + ")";
                                    }
                                }
                                html += ":" + entry.provinceName + entry.buyerCityName + entry.districtName + entry.community + entry.buyerAddress;
                                html += "(" + entry.itemDetail + ")";
                                if (entry.isPostpone == 1) {
                                    html += "<br/>(因" + entry.postponeReason + ",送达时间由"
                                        + CRM.formatDate(new Date(entry.lastRequireSendTime), "date") + "延期至"
                                        + CRM.formatDate(new Date(entry.requireSendTime), "date") + ")";
                                }
                                html += "<ul style='margin-left: 18px;'>";
                                html += "<li>收货人：" + entry.buyerNick + "</li>";
                                html += "<li>收货人号码：" + entry.buyerMobile + "</li></ul>";
                                html += "</label>";
                                if (logisticsStatus && "待发货" != logisticsStatus) {
                                    html += "</a>";
                                }
                            });
                            if (_canModify) {
                                html += '<br/><button type="button" class="btn btn-info" onclick="javascript:openLogisticModify()">修改物流</button>';
                            }
                        } else {
                            if (item.logisticsDOList) {
                                $.each(item.logisticsDOList, function (index, logistics) {
                                    var _reText = "";
                                    if (logistics.isRe) {
                                        _reText = "重发物流单-";
                                    }
                                    if (JuicerCommand.checkSendLogisticsnos(logistics.status) != "待发货") {
                                        html += "<br/><a id='pre_a_" + logistics.logisticsNo + "' href=javascript:queryLogistics('" + logistics.logisticsNo + "','" + logistics.company + "')>" + logistics.companyName + ":" + logistics.logisticsNo + "</a>(" + _reText + JuicerCommand.checkSendLogisticsnos(logistics.status) + ")";
                                    } else {
                                        var _statusText = "";
                                        if (logistics.status) {
                                            _statusText = "(" + _reText + JuicerCommand.checkSendLogisticsnos(logistics.status) + ")"
                                        } else {
                                            if (_reText) {
                                                _statusText = "(" + _reText + ")";
                                            }
                                        }
                                        html += "<br/>" + logistics.companyName + ":" + logistics.logisticsNo + _statusText;
                                    }
                                })
                            }
                        }
                    } else {
                        CRM.showDialog(req.message);
                    }
                }
            });
            return html;
        },
        /**
         * 预售单物流状态
         * @param sendLogisticsnos
         */
        checkSendLogisticsnos: function (sendLogisticsnos) {
            if (sendLogisticsnos == -1) {
                return "待发货";
            } else if (sendLogisticsnos == 0) {
                return "待发货";
            } else if (sendLogisticsnos == 1) {
                return "已发货";
            } else if (sendLogisticsnos == 2) {
                return "已发货";
            } else if (sendLogisticsnos == 3) {
                return "已发货";
            } else if (sendLogisticsnos == 4) {
                return "派件中";
            } else if (sendLogisticsnos == 5) {
                return "已签收";
            } else if (sendLogisticsnos == 6) {
                return "用户拒签";
            } else if (sendLogisticsnos == 7) {
                return "疑难";
            } else if (sendLogisticsnos == 8) {
                return "退回";
            } else if (sendLogisticsnos == 9) {
                return "滞留";
            } else if (sendLogisticsnos == 10) {
                return "回退入库";
            } else if (sendLogisticsnos == 11) {
                return "回退出库";
            } else if (sendLogisticsnos == 12) {
                return "退货出站扫描";
            } else {
                return "";
            }
        },
        /**
         * 订单来源渠道
         * @param from
         */
        showChannel: function (from) {
            var returnStr = "微信";
            if (from == 1) {
                returnStr = "APP";
            }
            return returnStr;
        }
    };

    return JuicerCommand;
});
